package com.kifas.service.dataaccess;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.TypeAkunEntity;

@Repository
public class TypeAkunDao {

	@Autowired
	private SessionFactory sessionFactory;

	private final static String ALL_TYPE_AKUN = "from TypeAkunEntity ta ";
	private final static String TYPE_AKUN_BY_ID = ALL_TYPE_AKUN
			+ "where ta.idType = :idType";

	public void insert(TypeAkunEntity typeAkun) throws Exception {
		sessionFactory.getCurrentSession().save(typeAkun);
	}

	public void update(TypeAkunEntity typeAkun) throws Exception {
		sessionFactory.getCurrentSession().update(typeAkun);
	}

	@SuppressWarnings("unchecked")
	public List<TypeAkunEntity> getTypeAkuns() {
		return sessionFactory.getCurrentSession().createQuery(ALL_TYPE_AKUN)
				.list();
	}

	public TypeAkunEntity getTypeAkun(Long idType) {
		return (TypeAkunEntity) sessionFactory.getCurrentSession()
				.createQuery(TYPE_AKUN_BY_ID).setLong("idType", idType)
				.uniqueResult();
	}
}
